package cn.mifengkong.appcommon.webview.jshandler;

import android.app.Activity;
import android.content.Context;

import com.mifengkong.frtools.jsbridge.CallBackFunction;
import com.mifengkong.frtools.jsbridge.JsHandler;
import com.mifengkong.statistic.umengsignin.WeChatSignInManager;

import java.util.List;

import cn.mifengkong.appcommon.util.gson.GsonTools;
import cn.mifengkong.appcommon.webview.listener.MyUMShareListener;
import cn.mifengkong.appcommon.webview.listener.SharePromptDialogListener;

/**
 * Created by jiang on 2018/3/6
 *
 * @author fengrong
 *         调起umeng分享
 */

public class UmengShareHandler implements JsHandler, SharePromptDialogListener {

    private Activity mActivity;
    private MyUMShareListener mListener;

    public UmengShareHandler(Context context) {
        if (context instanceof Activity) {
            mActivity = (Activity) context;
        }
    }

    @Override
    public void OnHandler(String handlerName, String responseData, CallBackFunction function) {
        ShareBean shareBean = GsonTools.changeGsonToBean(responseData, ShareBean.class);
        if (shareBean != null && mActivity != null) {
            switch (shareBean.type) {
                case 1://图片
                    WeChatSignInManager.with(mActivity)
                            .shareImageUrl(shareBean.thumImage)
                            .shareThumpImg(shareBean.thumImage)
                            .shareView(getMyUMShareListener(mActivity));
                    break;
                case 2://
                    break;
                case 0://链接
                default://链接
                    WeChatSignInManager.with(mActivity)
                            .shareTitle(shareBean.title)
                            .shareDesc(shareBean.describe)
                            .shareUrl(shareBean.link)
                            .shareThumpImg(shareBean.thumImage)
                            .shareView(getMyUMShareListener(mActivity));
                    break;
            }
        }
    }

    private MyUMShareListener getMyUMShareListener(Activity activity) {
        mListener = new MyUMShareListener(activity);
        return mListener;
    }

    @Override
    public void dialogDismiss() {
        if (mListener != null) {
            mListener.dialogDismiss();
        }
    }

    class ShareBean {
        int type = 0;//0（链接） / 1（图片） / 2（正常） //可不传，默认0，正常分享
        String title;
        String link;
        String thumImage;
        String describe;
        List<String> platform;//分享类别，可不传，默认QQ和微信

        @Override
        public String toString() {
            return "ShareBean{" +
                    "type=" + type +
                    ", title='" + title + '\'' +
                    ", link='" + link + '\'' +
                    ", thumImage='" + thumImage + '\'' +
                    ", describe='" + describe + '\'' +
                    ", platform=" + platform +
                    '}';
        }
    }
}
